<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!doctype html>
<html>
	<head>
		<title>激活码列表</title>
		<base href="<%=basePath%>" />
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="renderer" content="webkit">
		<meta http-equiv="Cache-Control" content="no-siteapp" />
		<meta name="apple-mobile-web-app-title" content="Amaze UI" />
		<link rel="stylesheet" href="layui/css/layui.css" media="all"> <!-- 适应所有设备 -->
		<style type="text/css">
		</style>
	</head>
	<% String nowDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); %>
	<body>
    <br/>
	<form class="layui-form" action="" onsubmit='return false;'>
		<div class="layui-form-item">
			<div class="layui-inline">
				<div class="layui-input-inline">
					<input type="text" name="FilterName" id="FilterName" placeholder="分组名称"
						autocomplete="off" class="layui-input">
				</div>
			</div>

			<div class="layui-inline">
				<div class="layui-input-inline">
					<input type="text" class="layui-input" name="start" id="start"
						placeholder="开始" autocomplete="off">
				</div>
			</div>
			
			<div class="layui-inline">
				<div class="layui-input-inline">
					<input type="text" class="layui-input" name="end" id="end"
						placeholder="结束" autocomplete="off">
				</div>
			</div>

			<div class="layui-inline">
				<div class="layui-input-inline">
					<button class="layui-btn layui-btn-normal" data-type="reload"
						lay-filter="selectbyCondition" id="selectbyCondition">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</div>
	</form>
	<table class="layui-hide" id="ActivationCode" lay-filter="ActivationCode"></table>
	    
	    <script type="text/html" id="toolbar">
          <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="addData">增加</button>
          </div>
        </script>
	    
	    <script type="text/html" id="modify">
          <a class="layui-btn  layui-btn-xs" lay-event="see">查看</a>
          <a class="layui-btn  layui-btn-xs" lay-event="download">下载</a>
          <a class="layui-btn  layui-btn-danger layui-btn-xs" lay-event="del">废弃剩余未使用</a>
        </script>
	    
	    <script src="js/jquery.min.js"></script>
		<script src="layui/layui.js"></script>
	<script src="js/common/monitor_back.js"></script>

	<script>
		    layui.use(['layer','form','table','laydate'],function(){
		    	var layer = layui.layer,
		    	    form  = layui.form,
		    	    table = layui.table,
		    	    laydate = layui.laydate;
		    	
		    	 layer.config({
						extend: 'MySkin/style.css'
					});
		    	table.render({
		    		elem:'#ActivationCode',
		    		url:'activation/list',
		    		toolbar: '#toolbar', //开启头部工具栏，并为其绑定左侧模板
		    		defaultToolbar: [],  //隐藏右边工具栏
		    		method:'post',
		    		title:'渠道',
		    		skin: 'row',
		    		even: true,
		    		id:'AllTable',
		    		cellMinWidth : 40,
		    		request:{
		    			pageName:'PageIndex',
		    			limitName:'PageCount'
		    		},
		    		where:{
		    			FilterName:$("#FilterName").val(),
		    			start:$("#start").val(),
		    			end:$("#end").val()
		    		},
		    		cols:[[
		    			{field:'GroupId',title:'分组ID',style:'color:red'},
		    			{field:'GroupName',title:'分组名称',style:'color:blue'},
		    			{field:'GroupRewards',title:'奖励物品',style:'color:red'},
		    			{field:'CodeCountTotal',title:'总数量',style:'color:blue'},
		    			{field:'CodeCountUsed',title:'已使用',style:'color:red'},
		    			{field:'CodeCountAbandoned',title:'已废弃',style:'color:blue'},
		    			{field:'CreateTime',title:'创建时间 ',style:'color:#705628'},
		    			{fixed:'right',title:'操作 ',toolbar: '#modify',width:300}
		    			]],
		    			page : true,//分页
						response : {
							statusCode : 200
						//重新规定成功的状态码为 200，table 组件默认为 0
						},
						parseData : function(res) { //将原始数据解析成 table 组件所规定的数据
							return {
								"code" : res.status, //解析接口状态
								"msg" : res.message, //解析提示文本
								"count" : res.count, //解析数据长度
								"data" : res.data
							//解析数据列表
							};
						}
		    	});
		    	
		    	//监听头工具栏事件
		    	table.on('toolbar(ActivationCode)',function(obj){
					if (history.state == null){
						pushHistory();
					}
		    		switch(obj.event){
		    		case 'addData':
		    			var index = layer.open({
							type: 2,
							shadeClose: true,
							shade: 0.5,
							area: ['500px', '500px'],
							title: '增加激活码',
							skin: 'layer-ext-yourskin',
							content: 'activationCode/addActivationCode',
							success: function(layero,index,resp) {
								var body = layer.getChildFrame("body", index);
								/* var data1 ={Version: "3", Name: "小石测试渠道3"};
				    			//push数组中 添加 数据
				    			layui.table.cache.AllTable.push(data1);
				    			var data2 = layui.table.cache.AllTable;
				    			table.reload('AllTable',{
				    				//当table采用url异步获取数据时，想要赋值 数据，重载表格，要清空url
				    				url:"",
				    				data:data2
				    			}); */
							},error:function(){
								
							},yes:function(index,layero){
								var obj = $(layero).find("iframe")[0].contentWindow;
								console.log(index);
							},
							cancel:function () {
								window.history.back();
							}
						})
		    			
		    		break;
		    		}
		    	});
		    	
		    	
		    	//监听行工具事件
		    	table.on('tool(ActivationCode)',function(obj){
					if (history.state == null){
						pushHistory();
					}
		    		var data = obj.data;
		    		var GroupId = data.GroupId;
		    		if(obj.event === 'see'){
		    			var index = layer.open({
							type: 2,
							shadeClose: true,
							shade: 0.5,
							area: ['1500px', '800px'],
							title: '查看兑换记录',
							skin: 'layer-ext-yourskin',
							content: 'activationCode/seerecord',
							success: function(layero,index,resp) {
								var body = layer.getChildFrame("body", index);
								body.find("#GroupId").val(GroupId);
								/* var data1 ={Version: "3", Name: "小石测试渠道3"};
				    			//push数组中 添加 数据
				    			layui.table.cache.AllTable.push(data1);
				    			var data2 = layui.table.cache.AllTable;
				    			table.reload('AllTable',{
				    				//当table采用url异步获取数据时，想要赋值 数据，重载表格，要清空url
				    				url:"",
				    				data:data2
				    			}); */
							},error:function(){
								
							},yes:function(index,layero){
								var obj = $(layero).find("iframe")[0].contentWindow;
							},
							cancel:function () {
								window.history.back();
							}
						})
		    		}else if(obj.event === 'download'){
		    			var index = layer.open({
							type: 2,
							shadeClose: true,
							shade: 0.5,
							area: ['500px', '250px'],
							title: '下载兑换记录',
							skin: 'layer-ext-yourskin',
							content: 'activationCode/toDownload',
							success: function(layero,index,resp) {
								var body = layer.getChildFrame("body", index);
								body.find("#GroupId").val(GroupId);
							},error:function(){
								
							},
							cancel:function () {
								window.history.back();
							}
						});
		    		}else if(obj.event === 'del'){
		    			layer.confirm('确定要废弃激活码分组吗？',{
		    				btn:['确定','取消']
		    			},function(index){
		    				$.ajax({
		    					type:"POST",
		    				    url:'activationCode/Abandon',
		    				    async: true,
		    				    // type:"json", //返回的数据是json
		    				    data:{
		    				    	GroupId:GroupId
		    				    },
		    				    success:function(resp){
		    				    	if(resp == ""){
		    				    		layer.msg("废弃成功",{icon:1});
		    				    		setTimeout(function (){
				    						var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
				                            parent.layer.close(index); //再执行关闭 
										}, 1000);
				    					layui.table.reload('AllTable',{});
		    				    	}else{
		    				    		layer.msg(resp,{icon:2});
		    				    	}
		    				    },error:function(){
		    				    	layer.msg("废弃失败",{icon:2});
		    				    }
		    				});
		    			},function(){
							window.history.back();
		    			});
		    		}
		    	});
		    	
		    	 laydate.render({
		    		    elem: '#start',
		    		    type: 'datetime',
		    		    trigger: 'click'
		    		  });
		    	 
		    	 laydate.render({
		    		    elem: '#end',
		    		    type: 'datetime',
		    		    trigger: 'click'
		    		  });
		    	 
		    	 //根据条件查询表格数据重新加载
				  var active = {
					    reload: function(){
					    //获取用户名
					      //执行重载
					      table.reload('AllTable', {
					        page: {
					          curr: 1 //重新从第 1 页开始
					      }
					      //根据条件查询
					        ,where:{
				    			FilterName:$("#FilterName").val(),
				    			start:$("#start").val(),
				    			end:$("#end").val()
				    		}
					      });
					    return false;
					   }
					};
				  //点击搜索按钮根据用户名称查询
				  $('#selectbyCondition').on('click',
			          function(){
			              var type = $(this).data('type');
			              active[type] ? active[type].call(this) : '';
			          });
		    	
		    });
		</script>
    </body>
</html>